package com.laich.springcloud.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.laich.springcloud.dao.UserDao;
import com.laich.springcloud.entity.User;


@Service
public class UserService {

	private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
	
	@Autowired
	private UserDao userDao;

	public User create(String username, String password) {
		User user = new User();
		user.setUsername(username);
		String hash = encoder.encode(password);
		user.setPassword(hash);
		User u = userDao.save(user);
		return u;

	}
	
	public User findByUsername(String username) {
		return userDao.findByUsername(username);
	}

	public User getUser(String id) {
		long userId = Long.parseLong(id);
		User user=userDao.findById(userId).get();
		return user;
	}
}
